$ = function(el){
return document.getElementById(el);
}
delay = 1500;
canvasWidth = document.body.clientHeight;
canvasHeight = document.body.clientHeight;
var maintitle = document.title;

Polaroid = function(location, title, w, h, service){

	return {
		init: function() {
			title = title ? title.slice(0,26) : '';
			this.image = document.createElement('div');
			var j = document.createElement('div');
			j.className = 'image';
			img = document.createElement('img');
			var self = this;
			img.src = location;
			fakeImg = document.createElement('img');
			fakeImg.style.display = 'hidden'; 
			fakeImg.onload = function(){
				self.onLoad(self);
				$('loadingbay').removeChild(this);
			}
			fakeImg.src = location;
			$('loadingbay').appendChild(fakeImg);
			if(w > h){
				img.className = 'w';
			} else {
				img.className = 'h';
			}
			this.image.className = 'poly frame'+Math.floor(Math.random()*4);
			this.image.appendChild(j);
			j.appendChild(img);
			var b = document.createElement('div');
			b.innerHTML = title;
			b.className = 'title';
			this.image.appendChild(b);
			a = randomLocation();
			b.style.fontFamily = getFont();
			this.image.style.left = a[0]+'px';
			this.image.style.top = a[1]+'px';
			this.image.style.webkitTransform = 'rotate(-90deg) scale(1.2)';
			document.title = maintitle+" - "+title;
		},
		location: location,
		service: service,
		image: null,
		onLoad: function(image) {
			PolyQueue.push(image);
		},
		move: function(){
			document.body.appendChild(this.image);
			this.image.style.transitionDelay="2s";
			var offsetX = this.image.clientWidth;
			var offsetY = this.image.clientHeight;
			var newX = (parseInt(this.image.style.left)*-1)+((canvasWidth/2)+(Math.random()*500)-(offsetX/2)+50); 
			var newY = (parseInt(this.image.style.top)*-1)+((canvasHeight/2)+(Math.random()*150)-(offsetY/2)-50);
			var rotate = Math.random()*15;
			if(Math.random() > 0.5){
				var rotate = rotate*-1;
			}
			var scale = 0.8+(Math.random()/10);
			this.image.style.webkitTransform = 'translate('+newX+'px, '+newY+'px) rotate('+rotate+'deg) scale('+scale+')';
			$('moby').className = '';
			$('hyves').className = '';
			$('twitter').className = '';
			$(this.service).className = 'highlight';
		}
	}
}

randomLocation = function(){
	var x = canvasWidth + Math.floor(Math.random()*500)+500;
	var y = canvasHeight + Math.floor(Math.random()*500)+500;
	return new Array(x, y);
}

PolyQueue = new Array();
PolyRemoveQueue = new Array();
loader = true;

processQueue = function(){
	if(PolyQueue.length){
		var p = PolyQueue.shift();
		p.move();
		PolyRemoveQueue.push(p);
	}
	window.setTimeout('processQueue()', (2000+Math.random()*delay));
	window.setTimeout('processRemoveQueue()', (Math.random()*delay));
}
processRemoveQueue = function(){
	if(PolyRemoveQueue.length > 10){
		var p = PolyRemoveQueue.shift();
		document.body.removeChild(p.image);
		delete(p);
	}
}
getFont = function(){
	var Fonts = new Array('assign','grunge','aescrawl','carbon','zimmerman','girls','buttons','appetite','dad','abscissa');
	var r = Math.random()*(Fonts.length);
	return Fonts[Math.floor(r)];
}
window.setTimeout('processQueue()', (Math.random()*delay));
	
	